// 配置路由的地方
import Vue from "vue"
import VueRouter from "vue-router"
import store from '@/store'
// 使用
Vue.use(VueRouter)
// 先把VueRouter原型对象的push
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// 重写push|replace
VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject)
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject)
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}
// 配置并暴露路由
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home',
      component: () =>
        import('../pages/Home')
    },
    {
      path: "/home",
      component: () =>
        import('../pages/Home'),
      meta: {
        show: true
      }
    },
    {
      path: "/login",
      component: () =>
        import('../pages/Login'),
      meta: {
        show: false
      }
    },
    {
      path: "/register",
      component: () =>
        import('../pages/Register'),
      meta: {
        show: false
      }
    },
    {
      path: "/search/:keyWord?",
      name: 'search',

      component: () =>
        import('../pages/Search'),
      meta: {
        show: true
      }
    },
    {
      path: "/detail",
      name: 'detail',
      component: () =>
        import('../pages/Detail'),
      meta: {
        show: true
      }
    },
    {
      path: "/addCart",
      name: 'addCart',
      component: () =>
        import('../pages/AddCartSuccess'),
      meta: {
        show: true
      }
    },
    {
      path: "/shopCart",
      name: 'shopCart',
      component: () =>
        import('../pages/ShopCart'),
      meta: {
        show: true
      }
    },
    {
      path: "/trade",
      name: 'trade',
      component: () =>
        import('../pages/Trade'),
      meta: {
        show: true
      },
      beforeEnter:(to,from,next)=>{
        if(from.path == '/shopCart'){
          next()
        }else{
          next(false)
        }
      }
    },
    {
      path: "/pay",
      name: 'pay',
      component: () =>
        import('../pages/Pay'),
      meta: {
        show: true
      },
      beforeEnter:(to,from,next)=>{
        if(from.path == '/trade'){
          next()
        }else{
          next(false)
        }
      }
    },
    {
      path: "/paysuccess",
      name: 'paysuccess',
      component: () =>
        import('../pages/PaySuccess'),
      meta: {
        show: true
      }
    },
    {
      path: "/center",
      name: 'center',
      component: () =>
        import('../pages/Center'),
      meta: {
        show: true
      },
      children:[
        {
          path: 'myorder',
          component: () =>
            import('../pages/Center/myOrder'),
        },
        {
          path: 'grouporder',
          component: () =>
            import('../pages/Center/groupOrder'),
        },
        {
          path: '/center',
          redirect:'/center/myorder'
        }
      ]
    },

  ],
  scrollBehavior(to, from, savedPosition) {
    return { y: 0 }
  }
})

router.beforeEach(async (to, from, next) => {
  next()
  // 然后再进行判断
  let token = store.state.user.token
  let name = store.state.user.name
  if (token) {
    if (to.path == '/login') {
      next('/home')
    }else {
       if(name){
         next()
       }else {
        //  没有用户信息
         try {
           await store.dispatch('getUserInfo')
           next()
         } catch (e) { 
          //  token失效的情况
          await store.dispatch('loginOut')
          next('/login')
         }
       }
    }
  } else { 
    // 未登录
    let toPath = to.path
    if(toPath.indexOf('/trade')!=-1 || toPath.indexOf('/pay')!=-1 || toPath.indexOf('/paysuccess')!=-1 || toPath.indexOf('/center')!=-1){
      next('/login?redirect='+toPath)
    }else{
       next()
    }
  }
  return false
})
export default router